package com.chenx.myapplicationcom1

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.chenx.myapplicationcom1.ui.theme.MyApplicationCom1Theme
import com.skydoves.landscapist.glide.GlideImage

class MainActivity : ComponentActivity() {
    private val viewModel: ImageViewModel by viewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationCom1Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    //Greeting("Android")
                    ImageList(viewModel = viewModel)

                }
            }
        }

        viewModel.fetchImages("https://www.qq.com")

    }
}
@Composable
fun ImageList(viewModel: ImageViewModel) {
    val images by viewModel.images.collectAsState()

    LazyColumn(modifier = Modifier.padding(16.dp)) {
        items(images) { imageUrl ->
            GlideImage(
                imageModel = imageUrl,
                modifier = Modifier.padding(8.dp),
                // shows a placeholder ImageBitmap when loading.
                loading = {
                    Text("Loading...")
                },
                // shows an error ImageBitmap when the request failed.
                failure = {
                    Text("Image request failed.")
                }
            )
        }
    }
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {

    MyApplicationCom1Theme {
//        Greeting("Android123")

    }
}